package com.lsh.payment.core.service.refund.impl;

import com.lsh.base.common.exception.BusinessException;
import com.lsh.payment.core.model.payenum.RefundStatus;
import com.lsh.payment.core.model.payenum.RefundTaskStatus;
import com.lsh.payment.core.model.payenum.RefundTaskType;
import com.lsh.payment.core.model.refund.PayRefund;
import com.lsh.payment.core.model.refund.RefundTask;
import com.lsh.payment.core.service.refund.AbstractRefundOrderStatusService;
import com.lsh.payment.core.service.refund.IRefundTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Project Name: lsh-payment
 *
 * @author peter
 * @date 18/6/2
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service
@Slf4j
public class RefundOrderRefundSuccessServiceImpl extends AbstractRefundOrderStatusService {

    @Autowired
    private IRefundTaskService refundTaskService;

    @Override
    protected boolean validate(PayRefund payRefund, RefundStatus target) {

        return true;
    }

    @Override
    protected boolean beforeProcess(PayRefund payRefund) throws BusinessException {

        return true;
    }


    @Override
    protected PayRefund orderRefund4update(PayRefund payRefund, RefundStatus target) throws BusinessException {

        PayRefund payRefund4update = new PayRefund();
        payRefund4update.setPaymentRefundNo(payRefund.getPaymentRefundNo());
        payRefund4update.setRefundRecvAccout(payRefund.getRefundRecvAccout());
        payRefund4update.setRefundStatus(target.getValue());
        if(payRefund.getRefundAt() == null){
            payRefund4update.setRefundAt(new Date());
        }else{
            payRefund4update.setRefundAt(payRefund.getRefundAt());
        }
        payRefund4update.setChannelRefundId(payRefund.getChannelRefundId());
        payRefund4update.setUpdateAt(new Date());

        return payRefund4update;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    protected boolean afterProcess(PayRefund payRefund, boolean addJopFlag) throws BusinessException {

        if(!addJopFlag){
            return true;
        }

        RefundTask filter = new RefundTask();
        filter.setRefId(payRefund.getPaymentRefundNo());
        filter.setTaskType(RefundTaskType.REFUND_NOTIFY.getValue());
        if(refundTaskService.selectRefundTaskOne(filter) != null) {
            return true;
        }

        RefundTask refundTask = new RefundTask();
        refundTask.setRefId(payRefund.getPaymentRefundNo());
        refundTask.setCreatedAt(new Date());
        refundTask.setUpdatedAt(new Date());
        refundTask.setTimes(0);
        refundTask.setIsValid(1);
        refundTask.setTaskType(RefundTaskType.REFUND_NOTIFY.getValue());
        refundTask.setStatus(RefundTaskStatus.TASK_NEW.getValue());
        refundTask.setVenderId(payRefund.getVenderId());

        return refundTaskService.addRefundTask(refundTask) > 0;
    }

    @Override
    public RefundStatus getSourceStatus() {
        return RefundStatus.REFUND_ING;
    }

    @Override
    public RefundStatus getTargetStatus() {
        return RefundStatus.REFUND_SUCCESS;
    }
}
